package com.bootdo.order.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.redis.lock.CommonRedisLock;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.RecoverDO;
import com.bootdo.order.service.RecoverService;

/**
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-01-08 11:14:54
 */
 
@Controller
@RequestMapping("/orders/recover")
public class RecoverController {

	private final String PREFIX = "orders/recover";

	@Autowired
	private RecoverService recoverService;
	@Autowired
	protected StringRedisTemplate stringRedisTemplate;
	
	@GetMapping()
	@RequiresPermissions("orders:recover:recover")
	String recover(){
	    return PREFIX + "/list";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("orders:recover:recover")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RecoverDO> recoverList = recoverService.list(query);
		int total = recoverService.count(query);
		PageUtils pageUtils = new PageUtils(recoverList, total);
		return pageUtils;
	}
	
	@GetMapping("/edit/{id}")
	@RequiresPermissions("orders:recover:edit")
	String edit(@PathVariable("id") Long id,Model model){
		RecoverDO recover = recoverService.get(id);
		model.addAttribute("recover", recover);
	    return PREFIX + "/edit";
	}
	
	/**
	 * 审核
	 */
	@IpFilter
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("orders:recover:edit")
	public R update(RecoverDO recover){
		CommonRedisLock commonLock = new CommonRedisLock(stringRedisTemplate, "ORDERRECOVER" + recover.getId());
		try {
			if (commonLock.tryLock(3)) {
				if (recoverService.update(recover) > 0) {
					return R.ok();
				}
			}
			return R.error();
		} finally {
			commonLock.unlock();
		}
	}
	
}
